﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Csla.Xaml;
using Converter;
using System.Resources;
using System.Reflection;
using Authentication;


namespace HeThongThongTin.UI.SL
{
    public partial class MainPage : UserControl
    {

       
        public MainPage()
        {
            LocalizationConverter converter = new LocalizationConverter();
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Navigator.Current.ContentPlaceholder = this.PlaceHolder;
            Navigator.Current.ProcessInitialNavigation();
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.StartView,HeThongThongTin.UI.SL");
            Dispatcher.BeginInvoke(delegate { this.PlaceHolder.Focus(); });

            


        }
       
        private void DanhMucChung_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.DanhMuc.DanhMuc_ListView,HeThongThongTin.UI.SL");
            this.Description_TextBlock.Text = "DANH MỤC CHUNG";
            LoadUserName();
            
        }

        private void TrangChinh_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            this.Description_TextBlock.Text = "TRANG CHÍNH";
            LoadUserName();
        }

        private void TiepNhanHoSo_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.TiepNhanHoSo.TiepNhanHoSo_ListView,HeThongThongTin.UI.SL");
            this.Description_TextBlock.Text = "TIẾP NHẬN HỒ SƠ";
            LoadUserName();
        }

        private void QuanLyDauTu_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            this.Description_TextBlock.Text = "QUẢN LÝ ĐẦU TƯ";
            LoadUserName();
        }

        private void DoanhNghiep_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            this.Description_TextBlock.Text = "DOANH NGHIỆP";
            LoadUserName();
        }

        private void BaoCaoThongKe_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.BaoCaoThongKe.BaoCaoThongKe_ListView,HeThongThongTin.UI.SL");
            this.Description_TextBlock.Text = "BÁO CÁO THỐNG KÊ";
            LoadUserName();
        }

        private void HeThong_RadRibbonTab_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.HeThong.HeThong_ListView,HeThongThongTin.UI.SL");
            this.Description_TextBlock.Text = "HỆ THỐNG";
            LoadUserName();
        }
        public void LoadUserName()
        {
            if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
            {
                this.UserName_Hyperlink.Content = Csla.ApplicationContext.User.Identity.Name;
            }
            else
            {
                this.UserName_Hyperlink.Content = "Anonymous";
            }
        }

        private void SignInButton_Click(object sender, RoutedEventArgs e)
        {
            string id = this.MainPage_SignInInputGrid_TenDangNhap_TextBox.Text;
            string pass = this.MainPage_SignInInputGrid_MatKhau_PasswordBox.Password;

            Authentication.CustomIdentity.Login(id, pass, (o, ex) =>
            {
                if (ex.Error == null)
                {
                    var principal = new CustomPrincipal(ex.Object);
                    Csla.ApplicationContext.User = principal;
                    
                    this.SignInGrid.Visibility = Visibility.Collapsed;
                    this.SignOutGrid.Visibility = Visibility.Visible;
                    this.LoadUserName();

                    
                }
                else
                {
                    MessageBox.Show(ex.Error.Data.ToString());
                }
            });
        }

        private void SignOutHyperLink_Click(object sender, RoutedEventArgs e)
        {
            
            this.SignInGrid.Visibility = Visibility.Visible;
            this.SignOutGrid.Visibility = Visibility.Collapsed;

            
            if (Csla.ApplicationContext.User.Identity.IsAuthenticated)
            {
                Csla.ApplicationContext.User = new CustomPrincipal(CustomIdentity.CreateAnynomousIdentity());
            }
            this.MainPage_SignInInputGrid_MatKhau_PasswordBox.Password = "";
            this.MainPage_SignInInputGrid_TenDangNhap_TextBox.Text = "";
            this.LoadUserName();
            
        }

        private void UserName_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.HeThong.HT_QuanLyTaiKhoan.HT_QuanLyTaiKhoan_MainWindow,HeThongThongTin.UI.SL");
        }

        private void DangKy_Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Navigator.Current.Navigate("HeThongThongTin.UI.SL.HeThong.HT_DangKy.HT_DangKy_MainWindow,HeThongThongTin.UI.SL");
        }

    }
}
